package com.liuguang.search.service.impl;

import com.liuguang.api.article.ArticleService;
import com.liuguang.api.article.pojo.ArticleContentDo;
import com.liuguang.api.search.SearchService;
import com.liuguang.common.enums.CommonResultEnum;
import com.liuguang.common.pojo.CommonResult;
import com.liuguang.search.pojo.ArticleIndex;
import com.liuguang.search.repository.ArticleRepository;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.cluster.support.FailoverClusterInvoker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @ClassName: SearchServiceImpl
 * @Description：
 * @Author: wanghr
 * Date: 1:50 PM 2020/10/20
 * Version: 1.0
 */
@DubboService
public class SearchServiceImpl implements SearchService {
    private static final Logger logger = LoggerFactory.getLogger(SearchServiceImpl.class);
    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @DubboReference(timeout = 40000)
    private ArticleService articleService;

    @Override
    public CommonResult initRepository() {
        Integer pageNum = 1;
        Integer pageSize = 2;
        logger.info("========开始初始化 ES 库=========");
        long l = System.currentTimeMillis();
        int num = 0;
        while (pageSize == 2) {
            //在数据库获取文章数据
            CommonResult<List<ArticleContentDo>> allArticleData = articleService.getAllArticleData(pageNum, pageSize);
            if (!CommonResultEnum.SUCCESS.getCode().equals(allArticleData.getCode())) {
                return allArticleData;
            }
            List<ArticleContentDo> articleDoList = allArticleData.getData();
            pageSize = articleDoList.size();
            pageNum++;
            //将数据库文章数据转换为 ArticleIndex
            List<ArticleIndex> articleIndexList = articleDoList.stream().map(articleDo -> {
                ArticleIndex articleIndex = new ArticleIndex();
                articleIndex.setId(articleDo.getId());
                articleIndex.setArticleName(articleDo.getArticleName());
                articleIndex.setArticleAuthor(articleDo.getArticleAuthor());
                articleIndex.setArticleEpitome(articleDo.getArticleEpitome());
                articleIndex.setLikeCount(articleDo.getLikeCount());
                articleIndex.setCommentCount(articleDo.getCommentCount());
                articleIndex.setArticleContent(articleDo.getArticleContent());
                return articleIndex;
            }).collect(Collectors.toList());
            logger.info("转化为存储到ES库的数据：{}", articleIndexList);
            //存储数据到 Es库
            articleRepository.saveAll(articleIndexList);
            num++;
        }
        logger.info("========结束初始化 ES 库=========；共{}次，耗时：{}毫秒", num, System.currentTimeMillis() - l);
        Optional<ArticleIndex> id = articleRepository.findById(1);
        System.out.println("id = " + id);
        return CommonResult.success("初始化ElasticSearch Repository 成功");
    }

    @Override
    public CommonResult search() {

//        elasticsearchRestTemplate
        return null;
    }
}


















